//
//  ModelEntity.cpp
//  GSEngine
//
//  Created by Александр on 02.02.14.
//  Copyright (c) 2014 SNK. All rights reserved.
//

#include "ModelEntity.h"
#include <GSEngine/Objects/EntityManager.h>
#include <GSEngine/Main/GSApplication.h>

namespace GSEngine
{
    ModelEntity::ModelEntity()
    {
        setType(EModelEntity);
        m_model = new GSModel();
        m_texture = new GSTexture();
    }
    ModelEntity::ModelEntity(const char* path)
    {
        m_model = new GSModel();
        m_model->loadModel(path, "rb");
        int verCount = m_model->getVertexCount();
        int indCount = m_model->getIndicesCount();
        setIndicesCount(indCount);
        setVertexCoords(m_model->getVertexCoordsArray(), verCount);
        setIndices(m_model->getIndicesArray(), indCount);
        setTextureCoords(m_model->getTextureCoordsArray(), verCount);
        setNormalsCoords(m_model->getNormalsArray(), indCount);
        m_texture = new GSTexture();
    }
    ModelEntity::~ModelEntity()
    {
        delete m_model;
        delete m_texture;
    }
    GSTexture* ModelEntity::getModelTexture()
    {
        return m_texture;
    }
    void ModelEntity::setModelTexture(const char* path)
    {
        std::string absPath = GSEngine::GSApplication::getInstance()->getPlatform()->getResourcesPath() + "/" + path;
        getModelTexture()->loadTexture(absPath.c_str(), "rb");
        setTexture(getModelTexture());
    }
    ModelEntity* CreateModelEntity(const char* path)
    {
        std::string absPath = GSEngine::GSApplication::getInstance()->getPlatform()->getResourcesPath() + "/" + path;
        ModelEntity* model = new ModelEntity(absPath.c_str());
        EntityManager::getInstance()->addObject(model);
        return model;
    }
}
